﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Infragistics.Win.UltraWinGrid;
using System.Drawing;
using Infragistics.Win;
using System.Windows.Forms;

namespace Crystal.CustomControls
{
    public partial class CrystalUltraGrid : UltraGrid
    {

        //private string mEmptyDataText = string.Empty;
        //private Point mEmptyDataTextLocation = new Point(30, 30);
        //public Point EmptyDataTextLocation
        //{
        //    get { return mEmptyDataTextLocation; }
        //    set
        //    {
        //        mEmptyDataTextLocation = value;
        //        setEmptyMessageIfRequired();
        //    }
        //}

        //public string EmptyDataText
        //{
        //    get { return mEmptyDataText; }
        //    set
        //    {
        //        mEmptyDataText = value;
        //        setEmptyMessageIfRequired();
        //    }
        //}

        //private void setEmptyMessageIfRequired()
        //{
        //    removeExistingEmptyData();

        //    //if there are no rows, and if there is an EmptyDataText message, display it now.
        //    if (EmptyDataText.Length > 0 && Rows.Count == 0)
        //    {
        //        Label lbl = new Label();
        //        lbl.Name = "EmptyDataLabel";
        //        lbl.Text = EmptyDataText;
        //        lbl.Size = new Size(Width, 25);
        //        lbl.Location = EmptyDataTextLocation;
        //        ControlUIElement.Control.Controls.Add(lbl);
        //    }
        //}
        //private void removeExistingEmptyData()
        //{
        //    //any previous empty data messages?
        //    Control[] lblempty = Controls.Find("EmptyDataLabel", true);
        //    if (lblempty.Length > 0)
        //    {
        //        Controls.Remove(lblempty[0]);
        //    }

        //}

        public CrystalUltraGrid()
        {
            InitializeLayout += CrystalGrid_InitializeLayout;
        }
        private void CrystalGrid_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            //setEmptyMessageIfRequired();
            //Common Settings
            Infragistics.Win.Appearance appearance35 = new Infragistics.Win.Appearance();
            appearance35.FontData.Name = "Calibri";
            appearance35.FontData.SizeInPoints = 8F;
            appearance35.ForeColor = Color.White;
            appearance35.FontData.Bold = DefaultableBoolean.True;
            appearance35.TextHAlign = HAlign.Center;
            e.Layout.Override.HeaderAppearance = appearance35;

            //Resize Columns
            e.Layout.AutoFitStyle = AutoFitStyle.ExtendLastColumn;
            e.Layout.Override.AllowMultiCellOperations = AllowMultiCellOperation.Copy;
            //Set colors
            int[] Values = new int[3];
            if (CommonCustomControls.IsValidString(StyleManager.GetStyle("GridColHeadBackColor"), ref Values))
            {
                e.Layout.Override.HeaderAppearance.BackColor = Color.FromArgb(Values[0], Values[1], Values[2]);
                e.Layout.Override.HeaderAppearance.BackColor2 = Color.FromArgb(Values[0], Values[1], Values[2]);
                e.Layout.Override.RowSelectorAppearance.BackColor = e.Layout.Override.HeaderAppearance.BackColor;
                e.Layout.Override.RowSelectorAppearance.BackColor2 = e.Layout.Override.HeaderAppearance.BackColor;
            }
            if (CommonCustomControls.IsValidString(StyleManager.GetStyle("GridColHeadForeColor"), ref Values))
            {
                e.Layout.Override.HeaderAppearance.ForeColor = Color.FromArgb(Values[0], Values[1], Values[2]);
            }
            if (CommonCustomControls.IsValidString(StyleManager.GetStyle("GridSelCellBackColor"), ref Values))
            {
                e.Layout.Override.ActiveCellAppearance.BackColor = Color.FromArgb(Values[0], Values[1], Values[2]);
                e.Layout.Override.ActiveCellAppearance.BackColor2 = e.Layout.Override.ActiveCellAppearance.BackColor;
            }

            if (CommonCustomControls.IsValidString(StyleManager.GetStyle("GridSelRowBackColor"), ref Values))
            {
                e.Layout.Override.ActiveRowAppearance.BackColor = Color.FromArgb(Values[0], Values[1], Values[2]);
            }
            if (CommonCustomControls.IsValidString(StyleManager.GetStyle("GridSelRowForeColor"), ref Values))
            {
                e.Layout.Override.ActiveRowAppearance.ForeColor = Color.FromArgb(Values[0], Values[1], Values[2]);
            }
            if (CommonCustomControls.IsValidString(StyleManager.GetStyle("GridNonSelRowBackColor"), ref Values))
            {
                e.Layout.Override.RowAppearance.BackColor = Color.FromArgb(Values[0], Values[1], Values[2]);
            }
            if (CommonCustomControls.IsValidString(StyleManager.GetStyle("GridNonSelRowForeColor"), ref Values))
            {
                e.Layout.Override.RowAppearance.ForeColor = Color.FromArgb(Values[0], Values[1], Values[2]);
            }
            if (CommonCustomControls.IsValidString(StyleManager.GetStyle("GridNonSelAlternateRowBackColor"), ref Values))
            {
                e.Layout.Override.RowAlternateAppearance.BackColor = Color.FromArgb(Values[0], Values[1], Values[2]);
            }
            if (CommonCustomControls.IsValidString(StyleManager.GetStyle("GridNonSelAlternateRowForeColor"), ref Values))
            {
                e.Layout.Override.RowAlternateAppearance.ForeColor = Color.FromArgb(Values[0], Values[1], Values[2]);
            }
            if (CommonCustomControls.IsValidString(StyleManager.GetStyle("GridGroupByBackColor"), ref Values))
            {
                e.Layout.GroupByBox.Appearance.BackColor = Color.FromArgb(Values[0], Values[1], Values[2]);
                e.Layout.GroupByBox.Appearance.BackColor2 = Color.FromArgb(Values[0], Values[1], Values[2]);
            }
            if (CommonCustomControls.IsValidString(StyleManager.GetStyle("GridGroupByPromptBackColor"), ref Values))
            {
                e.Layout.GroupByBox.PromptAppearance.BackColor = Color.FromArgb(Values[0], Values[1], Values[2]);
                e.Layout.GroupByBox.PromptAppearance.BackColor2 = Color.FromArgb(Values[0], Values[1], Values[2]);
            }
            if (CommonCustomControls.IsValidString(StyleManager.GetStyle("GridGroupByPromptForeColor"), ref Values))
            {
                e.Layout.GroupByBox.PromptAppearance.ForeColor = Color.FromArgb(Values[0], Values[1], Values[2]);
            }
        }
    }
}
